<?php

namespace App\Strategies;

use App\Constants\CreditConstant;
use App\Strategies\AppStrategy;

/**
 * 用户公共策略
 *
 * Class UserStrategy
 * @package App\Strategies
 */
class UserStrategy extends AppStrategy
{

    /**
     * @param $ctime
     * @return mixed
     * @desc    是否在登录的时候选择身份
     */
    public static function fetchDisplay($ctime)
    {
        $time = strtotime('2016-8-12 00:00:00');
        $ctime = strtotime($ctime);
        if ($ctime <= $time)
        {
            //显示选择身份页面
            $data['display'] = 1;
        }
        else
        {
            $data['display'] = 0;
        }
        return $data;
    }

    /**
     * @desc 生成随机字符串
     * @param $length
     * @return null|string
     */
    public static function getRandChar($length, $format = 'ALL')
    {
        $str = null;
        //$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        switch ($format)
        {
            case 'ALL':
                $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            case 'NC':
                $strPol = '0123456789abcdefghijklmnopqrstuvwxyz';
                break;
            case 'CHAR':
                $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                break;
            case 'NUMBER':
                $strPol = '0123456789' . time() . mt_rand(100, 1000000);
                break;
            default :
                $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                break;
        }
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[mt_rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        return $str;
    }

    /**
     * @desc    
     * 判断用户使用的是ios还是android
     */
    public static function version()
    {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $version = 7; //PC
        if (strpos($user_agent, 'iPhone') || strpos($user_agent, 'iPad') || strpos($user_agent, 'iPod'))
        {
            $version = 5;
        }
        elseif (strpos($user_agent, 'Android'))
        {
            $version = 6;
        }
        elseif (strpos($user_agent, 'MicroMessenger'))
        {
            $version = 8;
        }
        return $version;
    }

    /**
     * @param $indent
     * @param $array1
     * @param $array2
     * @param $array3
     * @param $array4
     * @param $array5
     * @return mixed
     * 用户信息——进度
     */
    public static function fetchProgress($indent, $array1 = [], $array2 = [], $array3 = [], $array4 = [], $array5 = [])
    {
        $datas = array_merge($array1, $array2, $array3, $array4, $array5);
        $datas = array_filter($datas);
        $userInfoCounts = count($datas);

        if ($indent == 1)
        {
            $progress = bcmul(bcdiv($userInfoCounts, 21, 2), 100);
        }
        elseif ($indent == 2)
        {
            $progress = bcmul(bcdiv($userInfoCounts, 25, 2), 100);
        }
        elseif ($indent == 3)
        {
            $progress = bcmul(bcdiv($userInfoCounts, 25, 2), 100);
        }
        elseif ($indent == 4)
        {
            $progress = bcmul(bcdiv($userInfoCounts, 19, 2), 100);
        }
        else
        {
            $progress = CreditConstant::DEFAULT_EMPTY;
        }
        $res['userInfoCounts'] = $userInfoCounts;
        $res['progress'] = $progress;
        return $res;
    }

    /**
     * @param array $array1
     * @param array $array2
     * @param array $array3
     * @param array $array4
     * @param array $array5
     * 普通合并数组
     */
    public static function getMerges($array1 = [], $array2 = [], $array3 = [], $array4 = [], $array5 = [])
    {
        $datas = array_merge($array1, $array2, $array3, $array4, $array5);
        $datas = array_filter($datas);
        $userInfoCounts = count($datas);
        return $userInfoCounts ? $userInfoCounts : 0 ;
    }

}
